package ru.nsu.ccfit.shumkina.algorithm;

import ru.nsu.ccfit.shumkina.algorithm.adaptation.Algorithm;
import ru.nsu.ccfit.shumkina.algorithm.adaptation.PathsInfoCollector;
import ru.nsu.ccfit.shumkina.algorithm.adaptation.UsedPathsInfo;
import ru.nsu.ccfit.shumkina.algorithm.adaptation.profitcounter.AdaptationSummaryPrinter;
import ru.nsu.ccfit.shumkina.algorithm.adaptation.profitcounter.Profit;
import ru.nsu.ccfit.shumkina.algorithm.adaptation.profitcounter.ProfitBasedOnUsedPathsInfo;
import ru.nsu.ccfit.shumkina.algorithm.graphworks.DotGraphMaker;
import ru.nsu.ccfit.shumkina.algorithm.graphworks.DotGraphSaver;
import ru.nsu.ccfit.shumkina.algorithm.graphworks.Graph;
import ru.nsu.ccfit.shumkina.algorithm.graphworks.LogsGraphMaker;
import ru.nsu.ccfit.shumkina.algorithm.logworks.Logs;
import ru.nsu.ccfit.shumkina.algorithm.world.Settings;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.TreeMap;

/**
 * Created with IntelliJ IDEA.
 * User: Вера
 * Date: 28.04.13
 * Time: 16:00
 * To change this template use File | Settings | File Templates.
 */
public class Main {
    public static void main(String[] args) {
        /* if (args.length == 0) {
             //need description
             System.out.printf("Not enough args. Once there will be text how to use this.\n");
             return;
         }*/

        int idx = 0;
        FormatType inputGraphType = FormatType.UNKNOWN;
        String inputGraph = null;
        FormatType userLogsType = FormatType.UNKNOWN;
        String userLogs = null;
        FormatType outputGraphType = FormatType.UNKNOWN;
        String outGraph = null;

        String configFile = null;
        String outputLogs = null;
        String profitFile = null;

        // parse command line options
       /* while (idx + 1 < args.length) {
            if (args[idx].equals("-dot")) {
                inputGraphType = FormatType.DOT;
                inputGraph = args[idx + 1];
            }
            else if (args[idx].equals("-tgf")) {
                inputGraphType = FormatType.TGF;
                inputGraph = args[idx + 1];
            }
            else if (args[idx].equals("-graphFromLogFile")) {
                inputGraphType = FormatType.LOGFILE;
                inputGraph = args[idx + 1];
            }
            else if (args[idx].equals("-graphFromLogDir")) {
                inputGraphType = FormatType.LOGDIR;
                inputGraph = args[idx + 1];
            }
            else if (args[idx].equals("-userLogFile")) {
                userLogsType = FormatType.LOGFILE;
                userLogs = args[idx + 1];
            }
            else if (args[idx].equals("-userLogDir")) {
                userLogsType = FormatType.LOGDIR;
                userLogs = args[idx + 1];
            }
            else if (args[idx].equals("-config"))
                configFile = args[idx + 1];
            else if (args[idx].equals("-outDot")) {
                outputGraphType = FormatType.DOT;
                outGraph = args[idx + 1];
            }
            else if (args[idx].equals("-outTgf")) {
                outputGraphType = FormatType.TGF;
                outGraph = args[idx + 1];
            }
            else if (args[idx].equals("-expectedLogs"))
                outputLogs = args[idx + 1];
            else if (args[idx].equals("-profitFile"))
                profitFile = args[idx + 1];
            else {
                System.out.printf("ERROR: Unknown option: %s\n", args[idx]);
                return;
            }
            idx += 2;
        }    */

       /* if (inputGraph == null || userLogs == null) {
            System.out.printf("ERROR: Input graph or  user logs unspecified.\n");
            return;
        }
        if (outGraph == null && outputLogs == null && profitFile == null) {
            System.out.printf("ERROR: Nothing to do.\n");
            return;
        }    */
        //////////////////////////////////////////////////////
        /*
        inputGraphType = FormatType.LOGDIR;
        inputGraph = "C:\\diplom\\useful\\ug_backpathstat_2012-11";
        */
        /*inputGraphType = FormatType.DOT;
        inputGraph = "C:\\Users\\vera\\Downloads\\tree-full.dot";//"C:\\Users\\vera\\Documents\\diplom\\useful\\result.dot";
        userLogsType = FormatType.LOGFILE;
        userLogs = "C:\\Users\\vera\\Downloads\\log-30-days.txt";//"C:\\Users\\vera\\Documents\\diplom\\useful\\ug_backpathstat_2012-11\\ug_backpathstat.log.2012-11-01-10";
        *//////////////////////////////////////////////////////
        if (args.length < 4) {
            //need description
            System.out.printf("Not enough args.\n");
            return;
        } else {
            inputGraphType = FormatType.DOT;
            inputGraph = args[0];
            userLogsType = FormatType.LOGFILE;
            userLogs = args[1];
            outGraph = args[2];
            profitFile = args[3];
        }

        Graph menu = null;
        Graph menuForAdaptation = null;
        Settings settings = new Settings();
        TreeMap<String, UsedPathsInfo>  usedPaths = null;
        // parse config
        // parse menu graph
        if (inputGraphType == FormatType.LOGFILE) {
            Logs graphLogs = new Logs();
            graphLogs.logFilesNames = new ArrayList<String>();
            graphLogs.logFilesNames.add(inputGraph);
            menu = LogsGraphMaker.restoreMenuGraph(graphLogs);
            graphLogs = new Logs();
            graphLogs.logFilesNames = new ArrayList<String>();
            graphLogs.logFilesNames.add(inputGraph);
            menuForAdaptation = LogsGraphMaker.restoreMenuGraph(graphLogs);
        }
        else if (inputGraphType == FormatType.LOGDIR) {
            File dir = new File(inputGraph);
            Logs graphLogs = new Logs();
            graphLogs.logFilesNames = new ArrayList<String>();
            addAllFiles(graphLogs.logFilesNames, dir);
            menu = LogsGraphMaker.restoreMenuGraph(graphLogs);
            graphLogs = new Logs();
            graphLogs.logFilesNames = new ArrayList<String>();
            addAllFiles(graphLogs.logFilesNames, dir);
            menuForAdaptation = LogsGraphMaker.restoreMenuGraph(graphLogs);
        }
        else if (inputGraphType == FormatType.DOT) {
            menu = DotGraphMaker.restoreMenuGraph(inputGraph);
            menuForAdaptation = DotGraphMaker.restoreMenuGraph(inputGraph);
        }
        // parse user logs
        if (userLogsType == FormatType.LOGFILE) {
            Logs logs = new Logs();
            logs.logFilesNames = new ArrayList<String>();
            logs.logFilesNames.add(userLogs);
            usedPaths = PathsInfoCollector.collectInfo(logs, settings, menu);
        }
        // adapt this!
        Algorithm algorithm = new Algorithm();
        algorithm.adaptedMenuGraph = menuForAdaptation;
        algorithm.settings = settings;
        algorithm.usedPaths = usedPaths;
        algorithm.makeAdaptations();
        // make needed output
        //out graph file
        //DotGraphSaver.saveGraphToDot(menu, "C:\\Users\\vera\\Documents\\diplom\\useful\\result1.dot");
        DotGraphSaver.saveGraphToDot(menuForAdaptation, outGraph);
        // replaced paths
        Profit profit =
                ProfitBasedOnUsedPathsInfo.countProfitInfoAndAdaptedPaths(usedPaths, menu,
                        menuForAdaptation, algorithm.acceptedAdaptations);
        // print adaptation summary
        AdaptationSummaryPrinter.printSummaryToFile(profitFile, profit, usedPaths);

    }

    private static void addAllFiles(ArrayList<String> logs, File dir) {
        if (!dir.isDirectory())
            return;

        for(File f: dir.listFiles())
        {
            if (f.isDirectory())
                addAllFiles(logs, f);
            else if (f.isFile())
                logs.add(f.getAbsolutePath());
        }
    }
}
